//
//  HPForgetPwdViewController.m
//  HPZZTong
//
//  Created by Handpay on 14-3-27.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPForgetPwdViewController.h"
#import "HPConditionTextField.h"
#import "ZZTongUtility.h"

@interface HPForgetPwdViewController ()

@property(nonatomic, retain)HPConditionTextField* accountTextField;
@property(nonatomic, retain)HPConditionTextField* pwdTextField;
@property(nonatomic, retain)HPConditionTextField* rePwdTextField;
@property(nonatomic, retain)HPConditionTextField* verifyCodeTextField;
@end

@implementation HPForgetPwdViewController

#define ERRORTIPS            @"6-20位字母和数字的组合"

#define kAccountTag     0
#define kVerifyCodeTag  1
#define kPasswordTag    2
#define kRePasswordTag  3


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"找回密码";
    [super viewDidLoad];
    
    self.forgetPwdTableView.dataSource = self;
    
    [self.confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)initTableViewCell:(int)index{
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 44)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    
    HPConditionTextField* textField = [[HPConditionTextField alloc] initWithFrame:CGRectMake(110, 0, 200, 44)];
    textField.textColor = kNormalFontColor;
    textField.font = kNormalFont;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    switch (index) {
        case kAccountTag:
            desLabel.text = @"账号/手机号";
            self.accountTextField = textField;
            break;
        case kPasswordTag:
            desLabel.text = @"新密码";
            textField.placeholder = ERRORTIPS;
            textField.secureTextEntry = YES;
            self.pwdTextField = textField;
            break;
        case kRePasswordTag:
            desLabel.text = @"确认密码";
            textField.secureTextEntry = YES;
            self.rePwdTextField = textField;
            break;
        case kVerifyCodeTag:
            desLabel.text = @"短信验证码";
            textField.conditionType = HPConditionType_Numeric;
            textField.maxLength = 6;
            self.verifyCodeTextField = textField;
            
            UIButton* getVerifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
            getVerifyButton.frame = CGRectMake(215, 2, 100, 40);
            [getVerifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            UIColor* color = kVerChannel == VerChannelHeXin ? [UIColor whiteColor] : kNormalFontColor;
            [getVerifyButton setTitleColor:color forState:UIControlStateNormal];
            [getVerifyButton setBackgroundImage:[[UIImage imageNamed:@"smallbutton.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:0] forState:UIControlStateNormal];
            [getVerifyButton addTarget:self action:@selector(getVerifyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            getVerifyButton.titleLabel.font = kNormalFont;
            [cell addSubview:getVerifyButton];
            
            textField.frame = CGRectMake(110, 0, 105, 44);
            break;
        default:
            break;
    }
    
    [cell addSubview:desLabel];
    [desLabel release];
    
    [cell addSubview:textField];
    [textField release];
    
    return [cell autorelease];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self initTableViewCell:indexPath.row];
}

- (void)getVerifyButtonClick:(UIButton*)button{
    if (![ZZTongUtility checkIsInput:self.accountTextField tips:@"账号或手机号"]) {
        return;
    }
    
    ZZTongClientEngine* zzt = [ZZTongClientEngine sharedInstance];
    
    NSDictionary* params = @{KEY_ACTION:@"resetPwd", KEY_ACCOUNT:DES_ENC(self.accountTextField.text), KEY_CHANNEL: [zzt getChannel]};
    [zzt doSendVerifyCode:params withButton:button];
}

- (void)dealloc {
    self.accountTextField = nil;
    self.pwdTextField = nil;
    self.rePwdTextField = nil;
    self.verifyCodeTextField = nil;
    
    [_forgetPwdTableView release];
    [_confirmButton release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setForgetPwdTableView:nil];
    [self setConfirmButton:nil];
    [super viewDidUnload];
}

- (IBAction)confirmButtonClick:(id)sender {
    if (![ZZTongUtility checkIsInput:self.accountTextField tips:@"账号或手机号"]) {
        return;
    }
    
    if (![ZZTongUtility checkIsInput:self.verifyCodeTextField tips:@"短信验证码"]) {
        return;
    }
    
    if (![ZZTongUtility checkIsInput:self.pwdTextField tips:@"新密码"]) {
        return;
    }
    
    if (![ZZTongUtility checkIsInput:self.rePwdTextField tips:@"确认密码"]) {
        return;
    }

    if (![ZZTongUtility checkLength:self.accountTextField tips:@"请输入正确的账号或手机号" minLen:6 maxLen:20]) {
        return;
    }
    
    if (![ZZTongUtility checkLength:self.verifyCodeTextField tips:@"请输入6位短信验证码" minLen:6 maxLen:6]) {
        return;
    }
    
//    NSString* accountErrorTips = [@"账号请输入" stringByAppendingString:ERRORTIPS];
//    NSString* account = self.accountTextField.text;
//    if ([Utility checkChar:account type:HPCharType_Alpha] || [Utility checkChar:account type:HPCharType_Number]) {
//        [HPUIENG showAlert:accountErrorTips];
//        return;
//    }
    
    
    NSString* pwdErrorTips = [@"新密码请输入" stringByAppendingString:ERRORTIPS];
    NSString* pwd = self.pwdTextField.text;
    if ([Utility checkChar:pwd type:HPCharType_Alpha]
        || [Utility checkChar:pwd type:HPCharType_Number]
        || ![Utility checkChar:pwd type:HPCharType_Alphanumeric]) {
        [HPUIENG showAlert:pwdErrorTips];
        return;
    }
    
    if (![ZZTongUtility checkLength:self.pwdTextField tips:pwdErrorTips minLen:6 maxLen:20]) {
        return;
    }
    
    NSString* rePwd = self.rePwdTextField.text;
    if (![pwd isEqualToString:rePwd]) {
        [HPUIENG showAlert:@"两次输入的密码不一致，请重新输入"];
        return;
    }
    
    ZZTongClientEngine* zzt = [ZZTongClientEngine sharedInstance];
    
    NSDictionary* params = @{KEY_ACCOUNT: DES_ENC(self.accountTextField.text),
                             @"newPwd": DES_ENC(pwd),
                             KEY_ACTION: @"resetPwd",
                             KEY_VERIFYCODE: self.verifyCodeTextField.text,
                             KEY_CHANNEL:[zzt getChannel]};
    LOG(@"params:%@", params);
#if ZZT_FOR_TEST
    [self responseCallBackSuccess:NET_ACTION_MODIFY_ACCOUNT_PWD withData:nil];
#else
    [zzt.netEngine sendRequest:NET_ACTION_MODIFY_ACCOUNT_PWD withParams:params withDelegate:self];
    [zzt.uiEngine showNetWaiting];
#endif

}

- (void)responseCallBackSuccess:(NSString *)action withData:(NSDictionary *)respData{
    if ([action isEqualToString:NET_ACTION_MODIFY_ACCOUNT_PWD]) {
        
        AlertCommandData* commandCancel = [[AlertCommandData alloc] initCommand:@"返回登录" function:@selector(goBack)];
        AlertData* alertData = [[AlertData alloc] initAlertData:self title:@"提示" message:@"您的登录密码已重置成功！" commands:commandCancel, nil];
        [HPUIENG showAlertWithData:alertData];
        
        [commandCancel release];
        [alertData release];
    }
}

@end
